import 'package:flutter/material.dart';
import 'package:flutter/scheduler.dart';
import 'package:hi_cache/hi_cache.dart';
import 'package:hi_base/color.dart';
import 'package:net/utils/hi_constants.dart';

// 扩展
extension ThemeModeExtension on ThemeMode {
  String get value => <String>['System', 'Light', 'Dark'][index];
}

class ThemeProvider extends ChangeNotifier {
  ThemeMode _themeMode;
  var _platformBrightness = SchedulerBinding.instance.window.platformBrightness;

  // 监听系统dark mode变化---在home有对应的生命周期
  void darModeChange() {
    if (_platformBrightness !=
        SchedulerBinding.instance.window.platformBrightness) {
      _platformBrightness = SchedulerBinding.instance.window.platformBrightness;
      notifyListeners();
    }
  }

  // 判断是不是dark模式
  bool isDark() {
    if (_themeMode == ThemeMode.system) {
      // 获取系统的Dark Mode
      return SchedulerBinding.instance.window.platformBrightness ==
          Brightness.dark;
    }
    return _themeMode == ThemeMode.dark;
  }

  // 获取主题模式
  ThemeMode getThemeMode() {
    String theme = HiCache.getInstance().get(HiConstans.theme);
    switch (theme) {
      case 'Dark':
        _themeMode = ThemeMode.dark;
        break;
      case 'System':
        _themeMode = ThemeMode.system;
        break;

      default:
        _themeMode = ThemeMode.light;
        break;
    }
    // _themeMode = ThemeMode.dark;
    return _themeMode;
  }

// 设置主题
  void setTheme(ThemeMode themeMode) {
    // print(themeMode.value);
    HiCache.getInstance().setString(HiConstans.theme, themeMode.value);
    notifyListeners();
  }

  ThemeData getTheme({bool isDarkMode = false}) {
    var themeData = ThemeData(
      brightness: isDarkMode ? Brightness.dark : Brightness.light,
      errorColor: isDarkMode ? HiColor.dark_red : white,
      primaryColor: isDarkMode ? HiColor.dark_bg : white, //主题的主色调
      // 文字强调色
      accentColor: isDarkMode ? primary[50] : white, //主题的主色调
      // tab指示器颜色
      indicatorColor: isDarkMode ? primary[50] : white,
      // 页面背景色
      scaffoldBackgroundColor: isDarkMode ? HiColor.dark_bg : white, //主题的主色调
    );
    return themeData;
  }
}
